﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Diagnostics;
using System.Runtime.InteropServices;

namespace ClientModel
{
    public class Cls_TaskControl
    {
        List<Process> listProcess = new List<Process>();
        List<string> listBannedProcesses;

        public Cls_TaskControl()
        {
            listBannedProcesses = new List<string>();
            listBannedProcesses.Add("SnippingTool");
            listBannedProcesses.Add("StikyNot");
            listBannedProcesses.Add("Wisptis");
            listBannedProcesses.Add("mip");
            listBannedProcesses.Add("Magnify");
            listBannedProcesses.Add("Narrator");
            listBannedProcesses.Add("osk");
            listBannedProcesses.Add("TabTip");
            listBannedProcesses.Add("InputPersonalization");
            listBannedProcesses.Add("ehshell");
        }

        private const int CONST_SHOW = 5;
        private const int CONST_HIDE = 0;

        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);

        [DllImport("user32.dll")]
        private static extern int ShowWindow(IntPtr hwnd, int nCmdShow);

        public void ShowTaskbar()
        {
            ToggleVisibility(true);
        }

        public void HideTaskbar()
        {
            ToggleVisibility(false);
        }

        public void BlockTaskManager()
        {
            Process p = new Process();
            p.StartInfo.WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.System);
            p.StartInfo.FileName = "taskmgr.exe";
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            p.Start();

            listProcess.Add(p);
        }

        private static void ToggleVisibility(bool visible)
        {
            var taskbar = FindWindow("Shell_TrayWnd", null);
            var startmenu = FindWindowEx(taskbar, IntPtr.Zero, "Button", "Start");

            if (startmenu == IntPtr.Zero)
            {
                startmenu = FindWindow("Button", null);
            }

            ShowWindow(taskbar, visible ? CONST_SHOW : CONST_HIDE);

            ShowWindow(startmenu, visible ? CONST_SHOW : CONST_HIDE);
        }

        public void Unblock()
        {
            foreach (Process p in listProcess)
            {
                p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
                p.Kill();
            }
        }

        public bool FindAndKillProcess()
        {
            foreach (Process clsProcess in Process.GetProcesses())
            {
                foreach (var item in listBannedProcesses)
                {
                    if (item.ToString() == clsProcess.ProcessName)
                    {
                        clsProcess.Kill();
                        return true;
                    }
                }
            }

            return false;
        }
    }
}
